﻿using System.Security.Cryptography;
using System.Text;

namespace VueEleAdminFurion;
public class DESEncryptExtension
{
    private static string DESKey = $"{nameof(DESEncryptExtension)}1234567891011";

    public static string Encrypt(string Text)
        => Encrypt(Text, DESKey);

    public static string Decrypt(string Text)
    {
        if (!string.IsNullOrEmpty(Text))
            return Decrypt(Text, DESKey);
        else
            return "";

    }

    private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };


    public static string Encrypt(string encryptString, string encryptKey = null)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes((encryptKey ?? DESKey)[0..16]);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var DCSP = Aes.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch (Exception ex)
        {
            return ex.Message + encryptString;
        }

    }


    public static string Decrypt(string decryptString, string decryptKey = null)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes((decryptKey ?? DESKey)[0..16]);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            var DCSP = Aes.Create();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            Byte[] inputByteArrays = new byte[inputByteArray.Length];
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch (Exception ex)
        {
            return ex.Message + decryptString;
        }

    }
}
